package com.example.bookmanager;

import java.net.URLEncoder;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.xuyang.other.Flags;
import com.xuyang.other.ProgressDialog;

public class BookAddActivity extends Activity {
	private ImageView mImgFinish;
	private EditText mEditName,mEditCategory,mEditWriter,mEditPress,mEditLanguages,mEditIntroduce,mEditRemark;
	private Button mBtnSubmit;
	
	private RequestQueue mQueue;
	private AlertDialog mDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_book_add);
		init();
		mImgFinish.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mBtnSubmit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mDialog.show();
				String name=mEditName.getText().toString();
				String category=mEditCategory.getText().toString();
				String writer=mEditWriter.getText().toString();
				String press=mEditPress.getText().toString();
				String languages=mEditLanguages.getText().toString();
				String introduce=mEditIntroduce.getText().toString();
				String remark=mEditRemark.getText().toString();
				String url=Flags.BOOK_ADD.replace("NAME", URLEncoder.encode(name))
						.replace("CATEGORY", URLEncoder.encode(category))
						.replace("WRITER", URLEncoder.encode(writer))
						.replace("PRESS", URLEncoder.encode(press))
						.replace("LANGUAGES", URLEncoder.encode(languages))
						.replace("INTRODUCE", URLEncoder.encode(introduce))
						.replace("REMARKS", URLEncoder.encode(remark));
				StringRequest request=new StringRequest(url, new Response.Listener<String>() {
					@Override
					public void onResponse(String str) {
						mDialog.dismiss();
						if(str.equals("ok")){
							Toast.makeText(getBaseContext(), "添加书籍成功", 0).show();
						}else {
							Toast.makeText(getBaseContext(), "添加书籍失败", 0).show();
						}
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError arg0) {
						mDialog.dismiss();
						Toast.makeText(getBaseContext(), "请检查网络连接", 0).show();
					}
				});
				mQueue.add(request);		
			}
		});
		
	}
	private void init() {
		mImgFinish=(ImageView) findViewById(R.id.img_bookadd_finish);
		mEditName=(EditText) findViewById(R.id.edit_bookadd_name);
		mEditCategory=(EditText) findViewById(R.id.edit_bookadd_category);
		mEditWriter=(EditText) findViewById(R.id.edit_bookadd_writer);
		mEditPress=(EditText) findViewById(R.id.edit_bookadd_press);
		mEditLanguages=(EditText) findViewById(R.id.edit_bookadd_languages);
		mEditIntroduce=(EditText) findViewById(R.id.edit_bookadd_introduce);
		mEditRemark=(EditText) findViewById(R.id.edit_bookadd_remark);
		mBtnSubmit=(Button) findViewById(R.id.btn_bookadd_submit);
		mQueue=Volley.newRequestQueue(getApplicationContext());
		mDialog=new ProgressDialog(this).getDialog();
		mDialog.dismiss();
	}
	
}
